package cn.ad.server.web.filter.bid;

import cn.ad.server.web.spring.service.actions.context.BidContext;
import cn.ad.server.domain.ad.Strategy;
import cn.ad.server.domain.ad.StrategyCrowdMap;
import cn.ad.server.domain.enumration.Age;
import cn.ad.server.domain.enumration.Gender;

import java.util.List;

/**
 * 年龄定向（以最小值为基准）。性别定向。
 * @author wangwei
 *
 */
public class CrowdMatcher implements BidMatcherChain {

	public boolean match(Strategy strategy, BidContext context) {
		StrategyCrowdMap crowdTargert = strategy.getCrowdTargert();
		if(crowdTargert==null){			
			return true;
		}
		List<Age> ageList = crowdTargert.getAgeList();
		int ageLow = context.getAgeLow();
		if (!isMatchAge(ageLow, ageList)) {
			return false;
		}
		
		List<Gender> genderList = crowdTargert.getGenderList();
		int genderValue = context.getGender();
		if (!isMatchGender(genderValue, genderList)) {
			return false;
		}
		
		return true;
	}

	private boolean isMatchAge(int ageLow, List<Age> ageList) {
		if(ageList==null||ageList.size()==0){
			return true;
		}
		for (Age age : ageList) {
			if(age.isMatch(ageLow)){
				return true;
			}
		}
		return false;
	}

	private boolean isMatchGender(int genderValue, List<Gender> genderList) {
		if(genderList==null||genderList.size()==0){
			return true;
		}
		for (Gender gender : genderList) {
			if (genderValue==gender.getValue()) {
				return true;
			}
		}
		return false;
	}

}
